#include "stdafx.h"

extern "C" {
	#include "avcodec.h"
	#include "avformat.h"
	#include "swscale.h"
	#include "Frame.h"
};

void ConvertFrameToImage(Frame* frame, BYTE* dstBuffer)
{
	unsigned char * p1 ;
	unsigned char * p2 ;
	int h ;
	int h_stride ;
	int v ;
	int l;
	int i;
	int chroma_h;

	p1 = dstBuffer;
	p2 = frame->Y;
	h = frame->width;
	h_stride = frame->strideY;
	v = frame->height;
	l = frame->width;
	for(i = 0; i < v; i++, p1 += l, p2 += h_stride ) memcpy(p1, p2, h);
	p2 = frame->U;

	h /= 2;
	v /= 2;
	l /= 2;

	chroma_h = frame->strideCRGB;

	if( frame->format == PIX_FMT_YUV420P )
	{
		for(i = 0; i < v; i++, p1 += l, p2 += chroma_h) memcpy(p1, p2, h);
		p2 = frame->V;
		for(i = 0; i < v; i++, p1 += l, p2 += chroma_h) memcpy(p1, p2, h);
	}
	else if( frame->format == PIX_FMT_YUV422 )
	{
		for(i = 0; i < v; i++, p1 += l, p2 += 2*chroma_h) memcpy(p1, p2, h);
		p2 = frame->V;
		for(i = 0; i < v; i++, p1 += l, p2 += 2*chroma_h) memcpy(p1, p2, h);
	}
}

void getBGRAFromRGB(byte * srcBuffer, byte * dstBuffer, int nWidth, int nHeight)
{
	for (int i = 0; i < nWidth * nHeight; i++)
	{
		memcpy(dstBuffer, srcBuffer, 3);
		dstBuffer += 4;
		srcBuffer += 3;
	}
}

void YUV420toRGB24( unsigned char* src, int width, int height, unsigned char* dst )
{
	int				area = width * height;
	int				width_2 = width >> 1;
	int				width3 = width * 3;
	int				height_2 = height >> 1;
	unsigned char*	yline0 = src;
	unsigned char*	yline1 = src + width;
	unsigned char*	upos = src + area;
	unsigned char*	vpos = upos + (area >> 2);
	unsigned char*	rgbline0 = dst;
	unsigned char*	rgbline1 = dst + width3;

	for (int i = 0; i < height_2; i++) {
		for (int j = 0; j < width_2; j++) {
			unsigned char	y00 = *yline0++;
			unsigned char	y01 = *yline0++;
			unsigned char	y10 = *yline1++;
			unsigned char	y11 = *yline1++;
			unsigned char	u = *upos++;
			unsigned char	v = *vpos++;
			int				Y, U, V;
			int				R, G, B;

			U = u;	U -= 128;
			V = v;	V -= 128;

			Y = y00;	Y <<= 15;
			R = (Y + 44915 * V) >> 15;
			G = (Y - 22872 * V - 11064 * U) >> 15;
			B = (Y + 56769 * U) >> 15;
			if (R > 255)
				R = 255;
			if (G > 255)
				G = 255;
			if (B > 255)
				B = 255;
			if (R < 0)
				R = 0;
			if (G < 0)
				G = 0;
			if (B < 0)
				B = 0;
			*rgbline0++ = (unsigned char) (B);
			*rgbline0++ = (unsigned char) (G);
			*rgbline0++ = (unsigned char) (R);

			Y = y01;	Y <<= 15;
			R = (Y + 44915 * V) >> 15;
			G = (Y - 22872 * V - 11064 * U) >> 15;
			B = (Y + 56769 * U) >> 15;
			if (R > 255)
				R = 255;
			if (G > 255)
				G = 255;
			if (B > 255)
				B = 255;
			if (R < 0)
				R = 0;
			if (G < 0)
				G = 0;
			if (B < 0)
				B = 0;
			*rgbline0++ = (unsigned char) (B);
			*rgbline0++ = (unsigned char) (G);
			*rgbline0++ = (unsigned char) (R);

			Y = y10;	Y <<= 15;
			R = (Y + 44915 * V) >> 15;
			G = (Y - 22872 * V - 11064 * U) >> 15;
			B = (Y + 56769 * U) >> 15;
			if (R > 255)
				R = 255;
			if (G > 255)
				G = 255;
			if (B > 255)
				B = 255;
			if (R < 0)
				R = 0;
			if (G < 0)
				G = 0;
			if (B < 0)
				B = 0;
			*rgbline1++ = (unsigned char) (B);
			*rgbline1++ = (unsigned char) (G);
			*rgbline1++ = (unsigned char) (R);

			Y = y11;	Y <<= 15;
			R = (Y + 44915 * V) >> 15;
			G = (Y - 22872 * V - 11064 * U) >> 15;
			B = (Y + 56769 * U) >> 15;
			if (R > 255)
				R = 255;
			if (G > 255)
				G = 255;
			if (B > 255)
				B = 255;
			if (R < 0)
				R = 0;
			if (G < 0)
				G = 0;
			if (B < 0)
				B = 0;
			*rgbline1++ = (unsigned char) (B);
			*rgbline1++ = (unsigned char) (G);
			*rgbline1++ = (unsigned char) (R);
		}
		yline0 += width;
		yline1 += width;
		rgbline0 += width3;
		rgbline1 += width3;
	}
}